package cn.com.jslh.boarpro.main.view.fragment;


import android.Manifest;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.mindorks.nybus.NYBus;
import com.mindorks.nybus.annotation.Subscribe;
import com.mindorks.nybus.event.Channel;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.main.contract.AddTemplateContract;
import cn.com.jslh.boarpro.main.presenter.AddTemplatePresenterImpl;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class AddTemplateFragment extends BaseFragment implements AddTemplateContract.View {

    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private Bundle savedInstanceState;
    private AddTemplateContract.Presenter presenter;

    // TODO: Rename parameter arguments, choose names that match
    public AddTemplateFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            this.savedInstanceState = savedInstanceState;
        }
        presenter = new AddTemplatePresenterImpl(getContext(), this);
        NYBus.get().register(this, Channel.TWO);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        new RxPermissions(getActivity())
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (!aBoolean) {
                            Toast.makeText(getContext(), "未获取摄像头权限无法使用扫描二维码功能", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        View view = inflater.inflate(R.layout.fragment_add_template, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recycleview.setLayoutManager(linearLayoutManager);
        recycleview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        presenter.bindRV(recycleview);
        presenter.initShowPigInfoFragment(getFragmentManager());
        presenter.start();
        return view;
    }

    @Override
    public void setPresenter(AddTemplateContract.Presenter presenter) {

    }

    @OnClick(R.id.subbtn)
    public void onViewClicked() {
        presenter.subAction();
    }

    @Subscribe(channelId = Channel.TWO)
    public void onEvent(String s) {
        presenter.refresh();
//        presenter.bindRV(recycleview);
    }

    public static void notifyDatas() {
        NYBus.get().post("refresh", Channel.TWO);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NYBus.get().unregister(this, Channel.TWO);
    }
}
